const { ccclass, property } = cc._decorator;
import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./AbTestMgr"
import c from "./OutsideGuideExperimentMgr"
import u from "./PeripheralSystemDataMgr"
import p from "./EventMgr"
import d from "./GameEventType"
import h from "./MatchGameEventType"
import f from "./frameworkManager"
import y from "./SdkHelper"
import m from "./EngineUtil"
import _ from "./MatchGameData"
import v from "./MatchGameConfig"
import b, { AnimType } from "./BasePageCtrl"
import C from "./PageMgr"
import T from "./MatchGameSystemMgr"
import M from "./PeripheralSystemMgr"
import P from "./UiManage"
import S from "./OutSideLevelGuideExperimentPage"
import UiManager from "./UiManage";
import NativeUtils from "./NativeUtils";

@ccclass
export default class OutSideLevelGuideExperimentPageCtrl extends b {

    public isPlayDone = false;
    public step_text = "";
    public guide_step = 0;
    public onLoad() {
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = false;
        this._hasTouchLock = false;
        this._showWinSize = true;
        super.onLoad();
        this.addButtonListen();
        this.addEvent();
        this.initTestBtnShow();
        this.ui.maskNode.setContentSize(cc.winSize);
    };
    public initTestBtnShow() {
        this.ui.btn_close_guide.active = false;
    };
    public start() { };
    public addEvent() {
        p.listen(d.CITY_MOVE_DONE, this.canCilckContinue, this);
    };
    public removeEvent() {
        p.ignore(d.CITY_MOVE_DONE, this.canCilckContinue, this);
    };
    public onUILoad() {
        this.ui = this.node.addComponent(S);
    };
    public addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_continue, this.clickContinue, this, 0);
        UiManager.addButtonListen(this.ui.bottom, this.clickContinue, this, 0);
        UiManager.addButtonListen(this.ui.btn_begin_game, this.goToGame, this);
        UiManager.addButtonListen(this.ui.btn_close_guide, this.closeGuide, this);
        UiManager.addButtonListen(this.ui.btn_fund, this.clickMask, this);
        UiManager.addButtonListen(this.ui.cashNode, this.clickCash, this);
        UiManager.addButtonListen(this.ui.btn_day_click, this.clickColckDay, this);
    };
    public _init() {
        // this.dealGuideStep(); //暂时去掉新手引导
        this.hide();
    };
    public dealGuideStep() {
        var e = this;
        this.guide_step = u.getOutsideGuideStep();
        this.guide_step ? this.guide_step += 1 : this.guide_step = 1;
        this.dealOppReview();
        if (this.guide_step > c.OutSideLevelGuideExperimentStep.CLOCK_GUIDE_DONE) {
            f.log("基金实验超出引导步骤请检查=====");
            this.hide();
        } else {
            f.log("基金实验处理关卡外引导步骤=======", this.guide_step);
            this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_GUIDE ? p.trigger(d.GUIDE_CITY_MOVE) : this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_REWARD && p.trigger(d.GUIDE_CITY_REWARD_SHAKE);
            var t = u.break_through_succeed;
            if (this.guide_step != c.OutSideLevelGuideExperimentStep.FUND_GUIDE) if (this.guide_step != c.OutSideLevelGuideExperimentStep.FUND_PROVICE && this.guide_step != c.OutSideLevelGuideExperimentStep.CLICK_WITHDRAW) if (this.guide_step != c.OutSideLevelGuideExperimentStep.BREAKTHROUGH || t) if (this.guide_step != c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW) if (this.guide_step != c.OutSideLevelGuideExperimentStep.CLOCK_DAY) {
                this.step_text = c.getOutSideGuideExPerimentDes(this.guide_step);
                var a = this.ui.guideTip.getComponent(cc.RichText);
                a.string = "";
                this.ui.bottom.getComponent(cc.Layout).updateLayout();
                this.isPlayDone = false;
                this.unscheduleAllCallbacks();
                this.adjustUI();
                if (this.guide_step == c.OutSideLevelGuideExperimentStep.BREAKTHROUGH || this.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME) {
                    this.ui.guide_hand.active = false;
                    this.ui.btn_begin_game.active = false;
                    this.typingAni(a, this.step_text, 150, function () {
                        e.ui.guide_hand.active = true;
                        e.ui.btn_begin_game.active = true;
                        e.ui.btn_continue.active = false;
                        e.ui.bottom.getComponent(cc.Button).interactable = false;
                    });
                } else this.typingAni(a, this.step_text, 150, function () {
                    e.ui.btn_continue.active = true;
                    e.ui.btn_continue.getComponent(cc.Button).interactable = true;
                    e.ui.bottom.getComponent(cc.Button).interactable = true;
                });
                var o = this.getAudioPath();
                n.getInstance().playMusic(o);
            } else this.adjustUI(); else this.adjustUI(); else if (s.isNoGqtcFlag()) {
                this.ui.bgNode.active = true;
                this.ui.guide_hand.active = true;
                var i = c.getOutSideGuideExPerimentDes(this.guide_step);
                this.ui.guideTip.getComponent(cc.RichText).string = i;
                this.ui.bottom.getComponent(cc.Layout).updateLayout();
                this.ui.btn_begin_game.active = true;
                this.ui.btn_begin_game.getComponent(cc.Button).interactable = true;
            } else {
                this.ui.bgNode.active = false;
                this.ui.guide_hand.active = false;
                this.ui.btn_begin_game.active = false;
                this.goToGame();
            } else M.getTravelExtractInfo(); else {
                this.step_text = c.getOutSideGuideExPerimentDes(this.guide_step - 1);
                this.ui.guideTip.getComponent(cc.RichText).string = this.step_text;
                this.isPlayDone = true;
                NativeUtils.setVibrator();
                this.ui.btn_continue.active = false;
                this.ui.bottom.getComponent(cc.Button).interactable = false;
                this.adjustUI();
            }
        }
    };
    public getAudioPath() {
        var e = s.isDmthFlag(), t = u.isHitOppoReviewBan();
        if (this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_GUIDE && s.getDmth2Ab2()) return "outsideLevel_attractions_guide_2_palce";
        if (this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_GUIDE && e && !t) return "outsideLevel_experiment_guide_2_palce";
        var a = "outsideLevel_guide_" + this.guide_step;
        this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_REWARD || this.guide_step == c.OutSideLevelGuideExperimentStep.FUND_WITHDRAW || this.guide_step == c.OutSideLevelGuideExperimentStep.MOVE_REWARD || this.guide_step == c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW_TIPS ? a = "outsideLevel_experiment_guide_" + this.guide_step : this.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME ? a = "outsideLevel_guide_10" : this.guide_step == c.OutSideLevelGuideExperimentStep.CLOCK_GUIDE_DONE && (a = "outsideLevel_guide_13");
        return a;
    };
    public adjustUI() {
        this.ui.bgNode.active = true;
        this.ui.btn_fund.active = this.guide_step == c.OutSideLevelGuideExperimentStep.FUND_GUIDE;
        this.ui.cashNode.active = this.guide_step == c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW;
        this.ui.btn_day_click.active = this.guide_step == c.OutSideLevelGuideExperimentStep.CLOCK_DAY;
        this.ui.levelCrossingNode.active = false;
        this.ui.bgNode.active = true;
        this.guide_step == c.OutSideLevelGuideExperimentStep.CLOCK_DAY && (this.ui.btn_day_click.x = 280);
        if (this.guide_step <= c.OutSideLevelGuideExperimentStep.CITY_REWARD) {
            this.ui.bgNode.getComponent(cc.Widget).bottom = 35;
            this.ui.bgNode.getComponent(cc.Widget).updateAlignment();
            this.ui.guide_hand.active = false;
            this.ui.btn_begin_game.active = false;
            this.ui.maskNode.active = false;
            this.ui.btn_fund.active = false;
            if (this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_GUIDE || this.guide_step == c.OutSideLevelGuideExperimentStep.CITY_REWARD) {
                this.ui.btn_continue.active = false;
                this.ui.bottom.getComponent(cc.Button).interactable = false;
            }
        } else {
            this.ui.guide_hand.active = false;
            this.ui.btn_continue.active = true;
            this.ui.btn_continue.getComponent(cc.Button).interactable = true;
            this.ui.bottom.getComponent(cc.Button).interactable = true;
            this.ui.btn_begin_game.active = false;
            this.ui.maskNode.active = false;
            this.ui.btn_fund.active = false;
            if (this.guide_step == c.OutSideLevelGuideExperimentStep.FUND_GUIDE) {
                this.ui.btn_continue.active = true;
                this.ui.btn_continue.getComponent(cc.Button).interactable = false;
                this.ui.bottom.getComponent(cc.Button).interactable = false;
                this.ui.fundTagNode.getComponent(cc.Widget).updateAlignment();
                this.playGuideFundAction();
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.BREAKTHROUGH || this.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME) {
                this.guide_step == c.OutSideLevelGuideExperimentStep.BREAKTHROUGH && p.trigger(d.RECOVER_BEGIN_MAP);
                this.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME && this.updateTravelPro();
                this.ui.bgNode.getComponent(cc.Widget).bottom = 470;
                this.ui.bgNode.getComponent(cc.Widget).updateAlignment();
                this.ui.btn_begin_game.active = true;
                this.ui.btn_begin_game.getComponent(cc.Widget).updateAlignment();
                var e = this.ui.btn_begin_game.getPosition();
                this.ui.guide_hand.active = true;
                this.ui.guide_hand.x = e.x + 50;
                this.ui.guide_hand.y = e.y - .5 * this.ui.guide_hand.height + 20;
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.FUND_WITHDRAW || this.guide_step == c.OutSideLevelGuideExperimentStep.MOVE_REWARD) {
                this.ui.bgNode.getComponent(cc.Widget).bottom = 35;
                this.ui.bgNode.getComponent(cc.Widget).updateAlignment();
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW) {
                this.ui.bgNode.active = false;
                this.playGuideCashAction();
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW_TIPS) {
                this.ui.bgNode.getComponent(cc.Widget).bottom = 35;
                this.ui.bgNode.getComponent(cc.Widget).updateAlignment();
                this.ui.guide_hand.active = false;
                this.ui.btn_begin_game.active = false;
                this.ui.maskNode.active = false;
                this.ui.btn_fund.active = false;
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.CLOCK_DAY) {
                this.ui.bgNode.active = false;
                this.playGuideClockAction();
            } else if (this.guide_step == c.OutSideLevelGuideExperimentStep.CLOCK_GUIDE_DONE) {
                this.ui.bgNode.getComponent(cc.Widget).bottom = 35;
                this.ui.bgNode.getComponent(cc.Widget).updateAlignment();
                this.ui.guide_hand.active = false;
                this.ui.btn_begin_game.active = false;
                this.ui.maskNode.active = false;
            }
        }
    };
    public updateTravelPro() {
        var e = this, t = s.isNoGqtcFlag();
        if (s.isOptimizeTravelFundFlag()) {
            this.ui.fundIcon.active = false;
            this.ui.levelFundLabel.active = false;
        } else {
            if (!t) return;
            this.ui.fundIcon.active = true;
            this.ui.levelFundLabel.active = true;
        }
        this.ui.levelCrossingNode.active = true;
        var a = u.getTravelLocation(), o = a.location, i = "<color=#3A3D68>再闯</color><color=#FC0B0B> " + a.need + " </color><color=#3A3D68>关到</color><color=#FC0B0B> " + u.getCityNameByCityId(o) + " </color><color=#3A3D68>，奖励</color>";
        this.ui.levelCrossTips.getComponent(cc.RichText).string = i;
        setTimeout( () =>{
            e.adjustProTipSize(e.ui.levelCrossTips);
        });
        var r = u.getTravelRewardInfo(), n = r.gold, l = r.cash, c = "<size=34>" + m.getCashBalance(l) + "</size><size=24>元</size>", p = "<size=34>" + m.getCashBalance(n) + "</size><size=24>元</size>";
        this.ui.levelCashLabel.getComponent(cc.RichText).string = c;
        this.ui.levelFundLabel.getComponent(cc.RichText).string = p;
    };
    public adjustProTipSize(e) {
        var t = e.width, a = 1;
        t >= 354 && (a = 354 / t);
        e.scale = a;
    };
    public playGuideClockAction() {
        var e = this;
        this.ui.maskNode.active = true;
        this.ui.btn_day_click.active = true;
        this.ui.btn_day_click.getComponent(cc.Button).interactable = false;
        var t = u.getClockData(), a = t.current, o = t.total, i = (t.get_status,
            a >= o ? o : a);
        this.ui.clickDayProNode.getComponent(cc.Label).string = i + "/" + o;
        this.ui.maskNode.getComponent(cc.Button).interactable = false;
        cc.tween(this.ui.btn_day_click).to(.5, {
            scale: 1.2
        }).to(.5, {
            scale: 1
        }).call(function () {
            e.ui.btn_day_click.getComponent(cc.Button).interactable = true;
            var t = e.ui.btn_day_click.getPosition();
            e.ui.guide_hand.setPosition(t.x - 20, t.y - 30);
            e.ui.guide_hand.active = true;
            e.ui.btn_day_click.getComponent(cc.Widget).updateAlignment();
        }).start();
    };
    public playGuideCashAction() {
        var e = this;
        this.checkCanWithDraw();
        this.ui.maskNode.active = true;
        this.ui.cashNode.active = true;
        this.ui.cashNode.getComponent(cc.Button).interactable = false;
        this.ui.maskNode.getComponent(cc.Button).interactable = false;
        this.ui.cashCount.getComponent(cc.Label).string = m.getCashBalance(l.cash_balance) + "";
        cc.tween(this.ui.cashNode).to(.5, {
            scale: 1.2
        }).to(.5, {
            scale: 1
        }).call(function () {
            e.ui.cashNode.getComponent(cc.Button).interactable = true;
            var t = e.ui.cashNode.getPosition();
            e.ui.guide_hand.setPosition(t.x - 50, t.y);
            e.ui.guide_hand.active = true;
            e.ui.cashNode.getComponent(cc.Widget).updateAlignment();
        }).start();
    };
    public playGuideFundAction() {
        var e = this;
        this.ui.maskNode.active = true;
        this.ui.btn_fund.active = true;
        this.ui.btn_fund.getComponent(cc.Button).interactable = false;
        cc.tween(this.ui.btn_fund).to(.5, {
            scale: 1.2
        }).to(.5, {
            scale: 1
        }).call(function () {
            e.ui.btn_fund.getComponent(cc.Button).interactable = true;
            var t = e.ui.fundTagNode.getPosition();
            e.ui.guide_hand.setPosition(t.x - 50, t.y + .5 * e.ui.guide_hand.height + 10);
            e.ui.guide_hand.active = true;
            e.ui.btn_fund.getComponent(cc.Widget).updateAlignment();
        }).start();
    };
    public clickContinue() {
        if (this.isPlayDone) {
            this.guide_step == c.OutSideLevelGuideExperimentStep.FUND_WITHDRAW && C.hasShowPage("FundWithDrawExperimentPage") && C.hidePage("FundWithDrawExperimentPage");
            u.setOutsideStep(this.guide_step);
            M.reportOutsideGuideStep(this.guide_step);
            this.dealGuideStep();
        } else {
            this.unscheduleAllCallbacks();
            this.isPlayDone = true;
            this.ui.guideTip.getComponent(cc.RichText).string = this.step_text;
            var e = this.getAudioPath();
            n.getInstance().stopMusic(e, false);
            if (this.guide_step == c.OutSideLevelGuideExperimentStep.BREAKTHROUGH || this.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME) {
                this.ui.btn_continue.active = false;
                this.ui.bottom.getComponent(cc.Button).interactable = false;
                this.ui.btn_begin_game.active = true;
                this.ui.guide_hand.active = true;
            }
        }
    };
    public dealOppReview() {
        if (u.isHitOppoReviewBan() && this.guide_step == c.OutSideLevelGuideExperimentStep.MOVE_REWARD) {
            this.guide_step += 2;
            u.setOutsideStep(this.guide_step - 1);
            M.reportOutsideGuideStep(this.guide_step - 1);
        }
    };
    public canCilckContinue() {
        this.ui.btn_continue.getComponent(cc.Button).interactable = true;
        this.ui.bottom.getComponent(cc.Button).interactable = true;
    };
    public goToGame() {
        var e = this;
        f.log("点击去游戏开始闯关了");
        this.ui.guide_hand.active = false;
        this.hide();
        var t = s.isNoGqtcFlag(), a = s.isOptimizeTravelFundFlag();
        T.matchGameStart(null, function () {
            if (t) {
                p.trigger(h.LOAD_MATCH_SCENE);
                if (a && e.guide_step == c.OutSideLevelGuideExperimentStep.CONTINUE_GAME) {
                    u.setOutsideStep(e.guide_step);
                    M.reportOutsideGuideStep(e.guide_step);
                }
            } else {
                var o = _.getGameInfo();
                f.log("MatchGameData.getGameInfo", o);
                o.levelType !== v.GameLevelType.GoldCoin ? C.showPage("MatchGameTargetPage", {
                    level: o.level,
                    target: o.targetMatchData,
                    guide_step: e.guide_step
                }) : C.showPage("MatchGameMoneyPage", {
                    level: o.level,
                    target: o.targetMatchData,
                    guide_step: e.guide_step
                });
            }
        }, function () { });
    };
    public clickClose() {
        this.hide();
    };
    public clickMask() {
        f.log("点击遮罩了====");
        this.ui.guide_hand.active = false;
        u.setOutsideStep(this.guide_step);
        M.reportOutsideGuideStep(this.guide_step);
        M.getTravelExtractInfo();
    };
    public clickCash() {
        this.hide();
        y.reportData("guide_experiment_step _click_redPaper");
        M.getExtractInfo(false, false, true);
    };
    public clickColckDay() {
        M.getClockTaskList(false, true);
        p.trigger(d.DAY_AND_FUD_SHOW, {
            isShow: false
        });
        this.hide();
    };
    public checkCanWithDraw() {
        if (u.isCanWithDraw()) {
            this.ui.btn_payment.active = false;
            this.ui.btn_canPay.active = true;
        } else {
            this.ui.btn_payment.active = true;
            this.ui.btn_canPay.active = false;
        }
    };
    public typingAni(e, t, a, o) {
        for (var i = this, r = /<.+?\/?>/g, n = t.match(r), l = t.replace(r, "│").split("│"), s = [], c = 0, u = 0, p = l; u < p.length; u++) {
            if ("" !== (v = p[u])) {
                v = "$[" + c + "]";
                c += 1;
            }
            s.push(v);
        }
        for (var d = s.join("│"), h = 0; h < l.length; h++) if ("" === l[h]) {
            l.splice(h, 1);
            h -= 1;
        }
        for (; -1 !== d.search("│");) if (n[0]) {
            d = d.replace("│", n[0].toString());
            n.splice(0, 1);
        } else {
            d = d.replace("│", "");
            console.warn("matchArr not enough");
        }
        for (var f = [], g = new Array(c).fill(""), y = 0; y < l.length; y++) for (var m = 0, _ = l[y]; m < _.length; m++) {
            var v = _[m];
            g[y] = g[y] + v;
            var b = d;
            for (h = 0; h < c; h++) b = b.replace("$[" + h + "]", g[h]);
            f.push(b);
        }
        var C = 0, T = () => {
            if (!i.isPlayDone) if (C >= f.length) {
                i.isPlayDone = true;
                o && o();
            } else {
                e.string = f[C];
                C += 1;
                setTimeout( () =>{
                    T();
                }, a);
            }
        };
        setTimeout( () =>{
            T();
        }, a);
    };
    public closeGuide() {
        this.hide();
        M.reportOutsideGuideStep(c.OutSideLevelGuideExperimentStep.CONTINUE_GAME);
        u.setOutsideStep(c.OutSideLevelGuideExperimentStep.CONTINUE_GAME);
        p.trigger(d.CLOSE_OUTSIDE_GUIDE);
    };
    public hide() {
        u.getOutsideGuideStep() == c.OutSideLevelGuideExperimentStep.FUND_GUIDE && (this._animType = AnimType.NONE);
        super.hide.call(this);
    };
    public _onHide() {
        super._onHide.call(this);
        u.getOutsideGuideStep() == c.OutSideLevelGuideExperimentStep.FUND_GUIDE && (this._animType = AnimType.SCALE);
    };
    // t.prefabUrl = "OutSideLevelGuideExperimentPage";
    // t.className = "OutSideLevelGuideExperimentPageCtrl";
    // return r([A, O("UI/pages/OutSideLevelGuideExperimentPageCtrl")], t);
};

